//
//  ViewController.m
//  KITCHEN_STORIES
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "ViewController.h"



@interface ViewController ()<UIScrollViewDelegate>



{
    //创建页码控制器
    UIPageControl *page;
    
    //判断是不是第一次进入应用:
    BOOL flag;
}

/** 设置动态翻页属性 */
//@property (nonatomic, retain) CATransition *transition;

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    for (int i = 0; i < 3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"guidePage%d",i+1]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth * i, 0, ScreenWidth, ScreenHeight)];
        // 在最后一页创建按钮
        if (i == 2) {
            // 必须设置用户交互 否则按键无法操作
            imageView.userInteractionEnabled = YES;
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(ScreenWidth / 3, ScreenHeight * 7 / 8 + 20, ScreenWidth / 3, ScreenHeight / 16);
            [button setTitle:@"点击进入" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            //button.layer.borderWidth = 2;
            //button.layer.cornerRadius = 5;
           // button.clipsToBounds = YES;
           // button.layer.borderColor = [UIColor whiteColor].CGColor;
            
            [button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:button];
        }
        imageView.image = image;
        [myScrollView addSubview:imageView];
    }
    myScrollView.bounces = NO;
    myScrollView.pagingEnabled = YES;
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.contentSize = CGSizeMake(ScreenWidth * 3, ScreenHeight);
    myScrollView.delegate = self;
    [self.view addSubview:myScrollView];
    
    
    page = [[UIPageControl alloc] initWithFrame:CGRectMake(ScreenWidth / 3, ScreenHeight * 15 / 16, ScreenWidth / 3, ScreenHeight / 16)];
    // 设置页数
    page.numberOfPages = 3;
    // 设置页码的点的颜色
    page.pageIndicatorTintColor = [UIColor grayColor];
    // 设置当前页码的点颜色
    page.currentPageIndicatorTintColor = [UIColor whiteColor];
    
    [self.view addSubview:page];

    
//    //1.初始化
//    self.transition = [CATransition animation];
//    //2.设置动画时长,设置代理人
//    self.transition.duration = 1.5f;
//    self.transition.delegate = self;       //id  不用签协议
//    //3.设置切换速度效果
//    self.transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//    //4.动画切换风格
//    self.transition.type = kCATransitionReveal;       //淡入淡出
//    //5.动画切换方向
//    self.transition.subtype = kCATransitionFromRight;//右侧
    
    
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 计算当前在第几页
    page.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
}

// 点击按钮保存数据并切换根视图控制器
- (void) go:(UIButton *)sender{
    flag = YES;
    NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
    // 保存用户数据
    [useDef setBool:flag forKey:@"notFirst"];
    [useDef synchronize];
    // 切换根视图控制器
    self.view.window.rootViewController = self.tab;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
